﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Model.Result;
using Estsh.Core.Repositories;
using System.Collections;
using System.Data;
using System.Text;
using System.Security.Cryptography;
using System.Text.Json;
using Estsh.Core.Models;
using Newtonsoft.Json.Linq;
using Estsh.Core.Model.EnumUtil;

namespace Estsh.Core.Wms.Repositories
{
    public class OutPdlineReceiveRepository : BaseRepository<BaseEntity>, IOutPdlineReceiveRepository
    {
        public OutPdlineReceiveRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }

        /// <summary>
        /// 查询成品和半成品下线
        /// </summary>
        /// <returns></returns>
        public List<MesWmsPgReceive> GetOutPdlineReceiveInfo()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT top 3000 * FROM MES_WMS_PG_RECEIVE (nolock) ");
                sql.Append("WHERE  syn_flg = 10 "); //未同步：10  已同步：20
                sql.Append("ORDER BY create_time  ");

                return dbConn.Query<MesWmsPgReceive>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 成品下线 处理成品和半成品下线
        /// </summary>
        /// <param name="sapCu"></param>
        /// <returns></returns>
        public bool InsertOutPdlineReceiveInfo(List<MesWmsPgReceive> mesWmsPgs)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                for (int i = 0; i < mesWmsPgs.Count; i++)
                {
                    List<string> sqlList = new List<string>();
                    List<DynamicParameters> parametersList = new List<DynamicParameters>();

                    //回传到stock表
                    StringBuilder SqlStringBuilder = new StringBuilder(1024);
                    SqlStringBuilder.Append("select* from sys_stock where carton_no = '"+ mesWmsPgs[i].SerialNumber + "' ");//
                    SysStock sysStock = dbConn.Query<SysStock>(SqlStringBuilder.ToString()).FirstOrDefault();
                    if (sysStock!=null)
                    {
                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("update sys_stock set tray_no=@trayNo,channel_number=@channelNumber,mtoc=@mtoc,mtoc_desc=@mtocDesc,cartype_name=@carTypeName,update_userid=@updateUserid,update_time=@updateTime where carton_no=@cartonNo ");//更新同步状态
                        sqlList.Add(SqlStringBuilder.ToString());
                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@cartonNo", mesWmsPgs[i].SerialNumber);//ID
                        parameters.Add("@trayNo", mesWmsPgs[i].TrayNo);//
                        parameters.Add("@channelNumber", mesWmsPgs[i].ChannelNumber);//
                        parameters.Add("@mtoc", mesWmsPgs[i].Mtoc);//
                        parameters.Add("@mtocDesc", mesWmsPgs[i].MtocDesc);//
                        parameters.Add("@carTypeName", mesWmsPgs[i].CartypeName);//
                        parameters.Add("@updateUserid", 0);//更新人
                        parameters.Add("@updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//更新时间
                        parametersList.Add(parameters);

                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("update MES_WMS_PG_RECEIVE set syn_flg=@synFlg,syn_time=@synTime,update_userid=@updateUserid,update_time=@updateTime where serial_number=@serialNumber ");//更新同步状态
                        sqlList.Add(SqlStringBuilder.ToString());
                        parameters = new DynamicParameters();
                        parameters.Add("@serialNumber", mesWmsPgs[i].SerialNumber);//ID
                        parameters.Add("@synFlg", "20");//已同步
                        parameters.Add("@synTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@updateUserid", 0);//创建人
                        parameters.Add("@updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//创建时间
                        parametersList.Add(parameters);

                        bool staResult = ExecuteTransaction(sqlList, parametersList);
                        if (staResult == false)
                        {
                            sqlList = new List<string>();
                            parametersList = new List<DynamicParameters>();
                            SqlStringBuilder = new StringBuilder(1024);
                            SqlStringBuilder.Append("update MES_WMS_PG_RECEIVE set syn_flg=@synFlg,syn_time=@synTime,err_msg=@errMsg,update_userid=@updateUserid,update_time=@updateTime where serial_number=@serialNumber ");//更新同步状态
                            sqlList.Add(SqlStringBuilder.ToString());

                            parameters = new DynamicParameters();
                            parameters.Add("@serialNumber", mesWmsPgs[i].SerialNumber);//ID
                            parameters.Add("@synFlg", "30");//同步异常
                            parameters.Add("@synTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                            parameters.Add("@errMsg", "同步错误，请检查接口数据！");//错误
                            parameters.Add("@updateUserid", 0);//更新人
                            parameters.Add("@updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//更新时间
                            parametersList.Add(parameters);
                            ExecuteTransaction(sqlList, parametersList);
                            return false;
                        }
                    }
                    else
                    {
                        sqlList = new List<string>();
                        parametersList = new List<DynamicParameters>();
                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("update MES_WMS_PG_RECEIVE set syn_flg=@synFlg,syn_time=@synTime,err_msg=@errMsg,update_userid=@updateUserid,update_time=@updateTime where serial_number=@serialNumber ");//更新同步状态
                        sqlList.Add(SqlStringBuilder.ToString());

                        DynamicParameters parameters = new DynamicParameters();
                        parameters.Add("@serialNumber", mesWmsPgs[i].SerialNumber);//ID
                        parameters.Add("@synFlg", "30");//同步异常
                        parameters.Add("@synTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//同步时间
                        parameters.Add("@errMsg", "同步错误，条码不存在！");//错误
                        parameters.Add("@updateUserid", 0);//更新人
                        parameters.Add("@updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));//更新时间
                        parametersList.Add(parameters);
                        ExecuteTransaction(sqlList, parametersList);
                        return false;
                    }
                   
                }

                return true;
            }
        }

    }
}
